package com.gxuwz.medical.domain.pay;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JApplet;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.domain.person.Person;

public class Pay {
	private String receiptsno;
	private String ybcardno;
	private double money;
	private int year;
	private Date paytime;
	private String username;
	
	public Pay(String ybcardno, double money) {
		this.year = Integer.parseInt(new Timestamp(new Date().getTime()).toString().substring(0, 4));
		this.ybcardno = ybcardno;
		this.money = money;
	}
	public Pay(){
		this.year = Integer.parseInt(new java.sql.Date(new Date().getTime()).toString().substring(0, 4));
	}
	public void add(){
		createReceiptsNo();
		try{
			DbUtil db = new DbUtil();
			String sql ="insert into pay values('"+this.receiptsno+"','"+this.ybcardno+"',"+this.money+","+this.year+",'"+ new Timestamp(new Date().getTime()) +"','"+this.username+"')";
			System.out.println(sql);
			db.executeUpdate(sql);
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public List queryNoPayPerson(String familycode){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		List list = new ArrayList();
		try{
			list = new Person().queryByFamilycode(familycode, 1, 100).getPageData();
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql = "select * from person,pay where familycode='"+familycode+"' and year="+this.year+" and person.ybcardno=pay.ybcardno";
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				String ybcardno = rs.getString("ybcardno");
				for(int i=0;i<list.size();i++){
					Person person = (Person)list.get(i);
					if(ybcardno.equals(person.getYbcardno()))
						list.remove(i);
				}
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}
		return list;
	}

	public boolean isPayFinish(String familycode){//所有人缴费完成
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		int count = 0;
		try{
			conn = new DbUtil().getConn();
			stmt = conn.createStatement();
			String sql = "select count(*) from person,pay where familycode='"+familycode+"' and year="+this.year+" and person.ybcardno=pay.ybcardno";
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				count = rs.getInt(1);
				int should = new Person().queryByFamilycode(familycode, 1, 100).getPageData().size();
				if(count==should)
					return true;
				}
			return false;
		}catch(SQLException e){
			e.printStackTrace();
			return true;
		}
	}
	private void createReceiptsNo(){
		this.receiptsno = this.ybcardno+String.valueOf(new Date().getTime());
	}
	public String getReceiptsno() {
		return receiptsno;
	}
	public void setReceiptsno(String receiptsno) {
		this.receiptsno = receiptsno;
	}
	public String getYbcardno() {
		return ybcardno;
	}
	public void setYbcardno(String ybcardno) {
		this.ybcardno = ybcardno;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	public Date getPaytime() {
		return paytime;
	}
	public void setPaytime(Date paytime) {
		this.paytime = paytime;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	static public void main(String[]args){
		//System.out.println(new Pay().isPayFinish("4504210101010001"));
		
		System.out.println(new Pay().queryNoPayPerson(""));
	}
}
